<?php

/**
 * This is the model class for table "{{admin_right}}".
 *
 * The followings are the available columns in table '{{admin_right}}':
 * @property string $id
 * @property integer $parent_id
 * @property string $c
 * @property string $a
 * @property string $desc
 * @property integer $status
 * @property integer $dateline
 */
class HrAdminRight extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return HrAdminRight the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin_right}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('c, a, desc', 'required'),
			array('status, dateline', 'numerical', 'integerOnly'=>true),
			array('c, a, desc', 'length', 'max'=>64),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, c, a, desc, status, dateline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'c' => 'C',
			'a' => 'A',
			'desc' => 'Desc',
			'status' => 'Status',
			'dateline' => 'Dateline',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('c',$this->c,true);
		$criteria->compare('a',$this->a,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('dateline',$this->dateline);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function checkAccess($c , $a)
	{
		if(empty(Yii::app()->session['userGroup'])) return 0;
		if(!Yii::app()->session['userGroup']['admincp']) return 0;
		//获取当前的right id
		$r = $this->findByAttributes(array('c' => strtolower($c) , 'a' => strtolower($a)));
		if(!$r) return 1;
		//获取用户组的ids
		$g = HrAdminGroup::model()->findByPk(Yii::app()->session['userGroup']['admincp']);
		if(!$g) return 0;
		//比对
		$gArr = explode(',', $g->rights);
		if(in_array($r->id, $gArr)) 
			return 1;
		else 
			return 0;
	}
}